package com.whut.transportation;



import java.util.HashMap;

import com.whut.transportation.entity.User;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.overweight.OWHomeActivity;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.utils.Constant;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * 登陆处理
 * @author yuliyan
 */
public class LoginActivity extends BaseActivity{
	private EditText username_edit;
	private EditText password_edit;
	private Button signin_button;
	private User user;
	SharedPreferences preferences;//登陆后用户的信息存储
	Editor editor;//偏好编辑
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//登录按钮提交
		case R.id.signin_button:
			checkLogin();
			break;
		default:
			break;
		}
		super.onClick(v);
	}
	/**
	 * 检查登陆
	 * @param 用户名 密码
	 * @return 用户信息
	 */
	private void checkLogin() {
		String username = username_edit.getText().toString();
		String password = password_edit.getText().toString();
		editor.putString("username",username);
		editor.putString("password",password);
		/**
		 * 判断用户名或密码是否为空
		 */
		if(username.length()==0){
			Toast.makeText(mContext, "您输入的用户名为空", Toast.LENGTH_LONG);
		} else if(username.length()>0&&password.length()==0){
			Toast.makeText(mContext, "您输入的密码为空", Toast.LENGTH_LONG);
		} else {
			//用户名和密码不为空时，请求服务器 初始化请求参数类
			HttpRequestVo vo = new HttpRequestVo();
			vo.requestUrl = Constant.LOGIN;
			vo.context = mContext;
			//请求参数
			HashMap<String, String> req = new HashMap<String, String>();
			req.put("user.username", username.trim());
			req.put("user.password", password.trim());
			vo.requestDataMap = req;
			//返回数据解析类型
			vo.parser = new JSONParser(User.class);
			/**
			 * 向服务器发起请求 
			 * 采用post方法
			 */
			getDataFromServer(HttpManager.POST_MOTHOD, vo,
					new DataCallback<HashMap<String, Object>>() {
						/**
						 * 发出请求后到服务器返回前，执行的动作
						 */
						@Override
						public void onStart() {
							showProgressDialog(getResources().getString(
									R.string.loading_content));
						}
						/**
						 * 返回数据有误
						 */
						@Override
						public void onFailed() {
							showNetErrorDialog();
						}
						//服务器返回数据后
						@Override
						public void onFinish() {
							closeProgressDialog();
						}
						//服务器返回的数据
						@Override
						public void processData(
								HashMap<String, Object> paramObject,
								boolean paramBoolean) {
							//服务器返回的User数据
							user = (User) paramObject.get("result");
							//将用户表的主键id保存到偏好中
							editor.putString("id", String.valueOf(user.getId()));
							//将货运员或者超重处理员的主键保存到偏好之中
							editor.putString("userId", String.valueOf(user.getUserId()));
							//将操作员的类型存入偏好信息之中
							editor.putInt("flag", user.getFlag());
							editor.putInt("stationNo",user.getStationNo());
							editor.putString("account",user.getAccount());
							editor.putString("name",user.getName());
							
							//偏好数据提交
							editor.commit();
							//当flag标志值为2时表示登陆到货运员界面
							if(user.getFlag()==2){
								Intent huoyunyuan = new Intent(LoginActivity.this, HomeActivity.class);
								huoyunyuan.putExtra("user", user);
								startActivity(huoyunyuan);
								finish();
							} else if(user.getFlag() == 3){
								//当flag标志值为3时表示登陆到超重处理员界面
								Intent chaozhongyuan = new Intent(LoginActivity.this, OWHomeActivity.class);
								startActivity(chaozhongyuan);
								finish();
							}else if(user.getFlag() ==-1 || user.getFlag() == 0){
								//flag为-1或0表示提交数据有误或数据库中没有改用户
								Toast.makeText(mContext, "用户名或密码有误，请重新输入", Toast.LENGTH_LONG).show();
							}
						}
					});
		}
	}
	
	/**
	 * 查找组件ID
	 */
	@Override
	protected void findViewById() {
		username_edit = (EditText) findViewById(R.id.username_edit);
		password_edit = (EditText) findViewById(R.id.password_edit);
		signin_button = (Button) findViewById(R.id.signin_button);
	}
	/**
	 * 查找完Id后需要操作的内容
	 */
	@Override
	protected void afterFindViewId() {
		username_edit.setText(preferences.getString("username", null));
		password_edit.setText(preferences.getString("password", null));
	}
	/**
	 * 显示界面
	 */
	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.login);
		preferences = mContext.getSharedPreferences("userId", Context.MODE_PRIVATE);
		editor = preferences.edit();
	}
	/**
	 * 界面进入时读数据
	 */
	@Override
	protected void processLogic() {
		
	}
	/**
	 * 设置监听
	 */
	@Override
	protected void setListener() {
		signin_button.setOnClickListener(this);
		
	}
	/**
	 * 重新获取数据
	 */
	@Override
	protected void getDataAgain() {
		
	}
	
	
}
